package com.stx.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.stx.entity.Film;
import com.stx.entity.Film_comment;
import com.stx.entity.User;
import com.stx.entity.User_collection;
import com.stx.service.CommentService;
import com.stx.service.FilmService;
import com.stx.service.UserCollectionService;
import com.stx.service.UserService;

public class DojoAddComment extends ActionSupport {
	private CommentService cs;
	private UserService  us;
	private FilmService fs;
	private UserCollectionService ucs;
	private Film     film;
	private User      user;
	private Film_comment  comment;
	private User_collection userColle;
	private InputStream inputStream;
  

	public UserCollectionService getUcs() {
		return ucs;
	}

	public void setUcs(UserCollectionService ucs) {
		this.ucs = ucs;
	}

	public User_collection getUserColle() {
		return userColle;
	}

	public void setUserColle(User_collection userColle) {
		this.userColle = userColle;
	}

	public Film_comment getComment() {
		return comment;
	}

	public void setComment(Film_comment comment) {
		this.comment = comment;
	}

	public UserService getUs() {
		return us;
	}

	public void setUs(UserService us) {
		this.us = us;
	}

	public FilmService getFs() {
		return fs;
	}

	public void setFs(FilmService fs) {
		this.fs = fs;
	}

	public CommentService getCs() {
		return cs;
	}

	public void setCs(CommentService cs) {
		this.cs = cs;
	}

	public Film getFilm() {
		return film;
	}

	public void setFilm(Film film) {
		this.film = film;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public String addComment() throws UnsupportedEncodingException {
		/*
		 * 判断用户是否登录
		 * */
        String result;
        user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
        if(user==null){
        	result="请登录，然后提交评论";
        	inputStream = new ByteArrayInputStream(result.getBytes("utf-8"));
        	return "sessionerror";
        }else{
        	//通过id得到电影信息
        	film=fs.getFilm(film);
        	if(film!=null){
        		comment.setSc_time(new Date());
        		comment.setFilm(film);
        		comment.setUser(user);
        		cs.addCommentDao(comment);
        		result="提交成功";
        		/*电影评论+1*/
        		film.setFi_comments(film.getFi_comments()+1);
        		fs.updateFilm(film);
        		/*
        		 * 更新下session
        		 * */
        		user=us.getUserInformation(user);
        		ServletActionContext.getRequest().getSession()
				.setAttribute("user",user);
        		inputStream = new ByteArrayInputStream(result.getBytes("utf-8"));
        		return "success";
        	}else{
        		result="出错了";
        		inputStream = new ByteArrayInputStream(result.getBytes("utf-8"));
        		return "error";
        	}
        }
	}
	
	public  String addShouCang() throws UnsupportedEncodingException{
		  String result;
	        user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
	        if(user==null){
	        	result="请登录";
        		inputStream = new ByteArrayInputStream(result.getBytes("utf-8"));
	        	return "sessionerror";
	        }else{
	        	//通过id得到电影信息
	        	film=fs.getFilm(film);
	        	if(film!=null){
	        		userColle=new User_collection();
	        		userColle.setFilm(film);
	        		userColle.setUc_time(new Date());
	        		userColle.setUser(user);
	        		ucs.addCollection(userColle);
	        		result="收藏成功";
	        		inputStream = new ByteArrayInputStream(result.getBytes("utf-8"));
	        		/*
	        		 * 更新下session
	        		 * */
	        		user=us.getUserInformation(user);
	        		ServletActionContext.getRequest().getSession()
					.setAttribute("user",user);
	        		return "success";
	        	}else{
	        		result="出错了";
	        		inputStream = new ByteArrayInputStream(result.getBytes("utf-8"));
	        		return "error";
	        	}
	        }
		
	}
	
	
	public String findNewCollection() throws UnsupportedEncodingException{
		String result="还没有评论赶快行动吧";
		/**
		<h2>最新评论</h2>
        <img src="images/img_1.gif" alt="picture" width="97" height="89" hspace="20" vspace="5" />
        <p>是的，没错。今天我们就为您推荐了这一部《XXX》，我相信您一定会喜欢</p>
        <p><a href="#"><img src="images/read_more.gif" alt="picture" width="68" height="20" border="0" /></a></p>
		*/
		if(cs.getNewFilm_comment().size()!=0){
			comment=(Film_comment) cs.getNewFilm_comment().get(0);
			String url="filim_information?film.fi_id="+comment.getFilm().getFi_id();
			result="<h2>最新评论用户："+comment.getUser().getUs_nickname()+"</h2> <img src='/ytys/"+comment.getUser().getUs_image()+"' alt='picture' width='97' height='89' hspace='20' vspace='5' />"+
			"<p>"+comment.getSc_countent()+"</p>"+
			" <p><a href='"+url+"'>"+"  <img src='/ytys/images/zoom.gif' alt='picture' width='68' height='20' border='0' /></a></p>";
			
			System.out.println(result);
			inputStream = new ByteArrayInputStream(result.getBytes("utf-8"));
			return "success";
		}else{
			String url2="/ytys/jsp/login.jsp";
			result="<h2>还没有评论！</h2>"+"<p><a href='"+url2+"' >登陆，赶快行动</a></p>";
			inputStream = new ByteArrayInputStream(result.getBytes("utf-8"));
			return "error";
		}
	}
	}